#!/bin/sh

set +x
nsid=$1
orahome=$2
dgname=$3
store=$4
nodelist=$5
utiltime=$6

echo "***********************************"
echo "memory is : $totalmemory"
echo "***********************************"

if [ -z "$nsid" ]; then
   echo "missing SID name to clone"
   exit 1
fi

if [ -z "$orahome" ]; then
   echo "missing ORACLE_HOME"
   exit 1
fi

if [ -z "$dgname" ]; then
   echo "missing Diskgroup name"
   exit 1
fi

if [ -z "$store" ]; then
   echo "missing store directory"
   exit 1
fi

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

#get grid user name
guser=`ps -ef | grep -i pmon_+asm | grep -v grep | awk '{print $1}'`
israc="true"
if [ -z "$guser" ] && [ -z "$nodelist" ]; then
   echo "no ASM running, stand alone instance"
   israc="false"
fi

oruser=$username

if [ -z "$oruser" ]; then
   echo "missing Oracle user"
   exit 1
fi

#generate an asm_node_config file
. ${scriptsRoot}/oracle/oracleclone/OracleAppMountComFunc.sh
generate_asm_node_config $nodelist $oruser

if [ "$standalone" = "true" ]; then
   ${scriptsRoot}/oracle/asmclone/OracleASMDBClone_nonrac_tstamp.sh $nsid $orahome $dgname $store $oruser $guser $utiltime
else
   ${scriptsRoot}/oracle/asmclone/OracleASMDBClone.sh $nsid $orahome $dgname $oruser $store $guser $totalmemory $sgapct
fi

retval=$?

if [ $retval -ne 0 ]; then
   echo "ASM Clone failed"
   #always rewind upon failure
   if [ "$standalone" = "true" ]; then
      ${scriptsRoot}/oracle/asmclone/OracleASMDBRewind_nonrac.sh $nsid $orahome $oruser
   else
      ${scriptsRoot}/oracle/asmclone/OracleASMDBRewind.sh $nsid $orahome $oruser
   fi
fi

exit $retval
